package com.ht.medicaltreatment.activity

import androidx.activity.viewModels
import androidx.recyclerview.widget.LinearLayoutManager
import com.gym.baselib.base.BaseActivity
import com.ht.medicaltreatment.R
import com.ht.medicaltreatment.adapter.ProgressAdapter
import com.ht.medicaltreatment.adapter.ProgressIndexAdapter
import com.ht.medicaltreatment.bean.HaoCaiProgressBean
import com.ht.medicaltreatment.viewmodel.HaoCaiProgressViewModel
import kotlinx.android.synthetic.main.activity_haocai_progress_layout.recyclerView
import kotlinx.android.synthetic.main.activity_haocai_progress_layout.recyclerViewIndex
import kotlinx.android.synthetic.main.activity_haocai_progress_layout.tvHeJi

//耗材 使用进度
class HaoCaiProgressActivity:BaseActivity() {
    override fun getLayoutId() = R.layout.activity_haocai_progress_layout

    private val model by viewModels<HaoCaiProgressViewModel>()

    override fun init() {
        setTitle("耗材使用进度")
        showProgress()
        val linearLayoutManager =  LinearLayoutManager(this)
        linearLayoutManager.orientation = LinearLayoutManager.HORIZONTAL
        recyclerView.layoutManager = linearLayoutManager

        val adapter = ProgressAdapter()
        recyclerView.adapter = adapter

        recyclerViewIndex.layoutManager = LinearLayoutManager(this)
        val indexAdapter = ProgressIndexAdapter()
        recyclerViewIndex.adapter = indexAdapter


        val id = intent.getStringExtra("projectId")
        model.getProgress(id?:""){ bean ->
            dismissProgress()

            indexAdapter.setNewInstance(bean.data.projectConsumableRecordList)
            indexAdapter.addData(HaoCaiProgressBean.DataBean.ProjectConsumableRecordListBean())

            val topList = bean.data.projectConsumableList

            val timeBean = HaoCaiProgressBean.DataBean.ProjectConsumableListBean()
            timeBean.name = "时间"
            timeBean.id = -1

            val userBean = HaoCaiProgressBean.DataBean.ProjectConsumableListBean()
            userBean.name = "操作人"
            userBean.id = -2

            val list = arrayListOf(timeBean,userBean)
            topList.addAll(list)

            var qian = 0
            var hou = 0

            topList.forEachIndexed { topIndex, topListBean  ->
                val list = arrayListOf<String>()
                var daiNumber = 0
                //遍历底部数据集合
                bean.data.projectConsumableRecordList.forEachIndexed { bottomIndex,bottomBean ->
                    if("时间".equals(topListBean.name)){
                        var time = bottomBean.createTime
                        if(time == null){
                            time = ""
                        }
                        list += time
                    }else if("操作人".equals(topListBean.name)){
                        var username = bottomBean.username
                        if(username == null){
                            username = ""
                        }
                        list += username
                    }else if(bottomBean.projectConsumableRecordDetailsList != null && bottomBean.projectConsumableRecordDetailsList.size > topIndex){
                        val userNumber = bottomBean.projectConsumableRecordDetailsList[topIndex].useNumber
                        list += "$userNumber"
                        daiNumber += userNumber
                    }
                }

                hou += topListBean.number

                //计算待提供数据
                if(topListBean.id > 0){
                    qian += daiNumber
                    val shengYuNumber = topListBean.number - daiNumber
                    list += "$shengYuNumber"
                }else{
                    list += ""
                }
                topListBean.bottomBeanList = list
            }
            adapter.setNewInstance(topList)

            tvHeJi.text = "$qian/$hou"
        }
    }
}
